/*
 * Problem: 最小生成树
 * Author: Yuanshun L
 * Created: 23-Nov-2021
 * Topic: Prim算法
 */

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 1000+10;
const int INF = 10000000;
int cosmist[maxn];
int g[maxn][maxn]; // graph
int flag[maxn]; // if contained in the X set
int n,m; // the number of node and edge

void init(){
    for(int i=0;i<n;i++){
        cosmist[i] = INF;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            g[i][j]=INF;
        }
    }
    // 填充边信息
    int x,y,w;
    for(int i=1;i<=m;i++){
        cin >> x >> y >> w;
        g[x][y] = g[y][x] = w;
    }
}

// 打印邻接矩阵
void print(){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            printf("%8d ",g[i][j]);
        }
        printf("\n");
    }
}

void solve(){
    cosmist[0] = 0;
    int ans = 0; // 最短路径长度
    while(true){
        int v = -1;
        bool update = false;
        for(int i=0;i<n;i++){ // find the shortest edge
            if(!flag[i] && (v==-1 || cosmist[v] > cosmist[i])){
                v = i;
                update = true;
            }
        }
        if(!update) break;
        ans += cosmist[v];
        flag[v] = true;
        for(int i=0;i<n;i++){ // 更新与v相邻的节点
            cosmist[i] = min(cosmist[i],g[v][i]);
        }
    }
//    print();
    cout << "Short Path Value: " << ans << endl;
}

int main(){

    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);

    cin >> n >> m;
    init();
    memset(flag,0,sizeof(flag));
    solve();
    return 0;

}